(function (w) {/*全局函数*/

	var global={
		me:true,
		line:15,
		zis:[],
	}

	global.emit= function(name){
		var luoZi = new Event(name);
		luoZi.args = Array.prototype.slice.call(arguments,1);
		w.dispatchEvent(luoZi);
	}
	global.on = function (name,callback) {
		w.addEventListener(name,function (e) {
			callback.apply(w,e.args);
		});
	}

	for(var i =0;i<global.line;i++){
		global.zis[i]=new Array(global.line);
	}

	w.G = global;
})(window);

(function (w) {
	var chess = document.getElementById('chess');
	var ctx = chess.getContext('2d');

	var line = 15,width=30;

	for(var i =0;i<line;i++){
		var x=i * width+width/2,y1=width/2,y2=line*width-width/2;
		ctx.moveTo(y1,x);
		ctx.lineTo(y2,x);
		ctx.stroke();
		ctx.moveTo(x,y1);
		ctx.lineTo(x,y2);
		ctx.stroke();
	}


	function drawZi(x,y,black) {
		 x = (x+0.5)*width;
		 y = (y+0.5)*width;
		ctx.beginPath()
		ctx.arc(x,y,width/2,0,Math.PI*2)
		var rgl = ctx.createRadialGradient(x,y,width/3,x,y,width/10);
		if(black){
			rgl.addColorStop(0,'#333')
			rgl.addColorStop(1,'#ddd')
		}else{
			rgl.addColorStop(0,'#aaa')
			rgl.addColorStop(1,'#fff')
		}
		ctx.fillStyle= rgl;
		ctx.fill()
		ctx.closePath()
	}
	chess.onclick=function (e) {
		e.preventDefault();
		e.stopPropagation();
		w.G.emit('luo-zi',
			Math.floor(e.offsetX/width),
			Math.floor(e.offsetY/width));
	}
	w.drawZi = drawZi;
})(window);


(function (w) { //枚举所有赢法
	w.G.count = 0;
	w.G.wins = [];
	w.G.mywins = [];
	w.G.comwins = [];
	for(var i =0;i<15;i++){
		w.G.wins[i]=[];
		for(var j=0;j<15;j++){
			w.G.wins[i][j]=[];
		}
	}

	for(var i =0;i<15;i++){
		for(var j=0;j<11;j++){
			for(var k=0;k<5;k++){
				w.G.wins[i][j+k][w.G.count]=true;
			}
			w.G.count++;
		}
	}

	for(var i =0;i<15;i++){
		for(var j=0;j<11;j++){
			for(var k=0;k<5;k++){
				w.G.wins[j+k][i][w.G.count]=true;
			}
			w.G.count++;
		}
	}

	for(var i =0;i<11;i++){
		for(var j=0;j<11;j++){
			for(var k=0;k<5;k++){
				w.G.wins[i+k][j+k][w.G.count]=true;
			}
			w.G.count++;
		}
	}

	for(var i =0;i<11;i++){
		for(var j=14;j>3;j--){
			for(var k=0;k<5;k++){
				w.G.wins[i+k][j-k][w.G.count]=true;
			}
			w.G.count++;
		}
	}
	return;
})(window);


G.on('luo-zi',function (x,y) {
	// var x=e.x,y=e.y;
	if(G.zis[y][x])return;
	drawZi(x,y,G.me);
	G.zis[y][x] = G.me?1:2;//白子2，黑子1
	G.me = !G.me;
	if(!G.me)
		G.emit('luo-zi-over',x,y,G.mywins,G.comwins);
	else{
		G.emit('luo-zi-over',x,y,G.comwins,G.mywins);
	}
})

G.on('luo-zi-over',function (x,y,mwins,ywins) {
	for(var i in G.wins[y][x]){//遍历所有的赢法
		if(mwins[i]===false)continue;
		if(mwins[i]===undefined){
			mwins[i] = 0;
		}
		if(mwins[i]==4){
			alert('你赢了')
			G.emit('over');
			return;
		}
		mwins[i]++;
		ywins[i]=false;
	}	
	if(mwins===G.mywins)
	G.emit('pass-com');//电脑走棋
})

G.on('pass-com',function () {

	var score = [0,300,2000,10000];
	var scores=[];
	for(var i =0;i<15;i++){
		for(var j=0;j<15;j++){
			if(G.zis[i][j])continue;
			for(var k in G.wins[i][j]){
				if(G.comwins[k]===false&&G.mywins[k]===false){
					break;
				}
				if(G.comwins[k]==4){
					G.emit('luo-zi',j,i);
					return;
				}
				if(G.mywins[k]==4){
					G.emit('luo-zi',j,i);
					return;
				}
				if(!scores[k]){
					scores[k]={
						x:i,
						y:j,
						num:0
					}
					var arr =G.wins[i][j].filter((item,index)=>{
						if(!item)return false;
						if(!G.mywins[index]===false)return false;
						if(!G.comwins[index]===false)return false;
						return true;
					})
					scores[k].num += arr.length*33;

				}
				
				// if(G.mywins[k]==3)
				// debugger;
				G.mywins[k] && (scores[k].num+=score[G.mywins[k]]);
				G.comwins[k] && (scores[k].num+=score[G.comwins[k]]+800);
			}
		}
	}
	scores.sort(function (o,t) {
		return t.num-o.num;
	});
	// debugger;
	G.emit('luo-zi',scores[0].y,scores[0].x);

});
